package com.wowoai.common.basemodule;

import com.wowoai.common.enums.ResponseEnum;
import com.wowoai.common.response.ResponseVO;
import com.wowoai.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * 全局异常处理
 */
@RestControllerAdvice
@Slf4j
public class BasicExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public ResponseVO<Void> baseHandler(Exception e) {
        log.error("报错信息", e);
        return ResponseVO.fail(ResponseEnum.FAIL,e.getMessage());
    }

    /**
     * 参数校验异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResponseVO<Void> baseHandler(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        String errorMessage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMessage += fieldError.getDefaultMessage() + "!";
        }
        return ResponseVO.fail(ResponseEnum.BUSINESS_FAIL,errorMessage);
    }

    /**
     * 参数校验异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = BindException.class)
    public ResponseVO<Void> baseHandler(BindException e) {
        BindingResult bindingResult = e.getBindingResult();
        String errorMessage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMessage += fieldError.getDefaultMessage() + "!";
        }
        return ResponseVO.fail(ResponseEnum.BUSINESS_FAIL,errorMessage);
    }

    /**
     * 参数校验异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = ConstraintViolationException.class)
    public ResponseVO<Void> baseHandler(ConstraintViolationException e) {
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        Iterator<ConstraintViolation<?>> iterator = constraintViolations.iterator();
        List<String> msgList = new ArrayList<>();
        while (iterator.hasNext()) {
            ConstraintViolation<?> cvl = iterator.next();
            msgList.add(cvl.getMessageTemplate());
        }
        return ResponseVO.fail(ResponseEnum.BUSINESS_FAIL,String.join(",",msgList));
    }
    /**
     * 处理自定义的异常
     *
     * @param businessException
     * @return
     */
    @ExceptionHandler(value = BusinessException.class)
    public ResponseVO<Void> businessHandler(BusinessException businessException) {
        log.error("报错信息 = {}", JsonUtils.toJsonString(businessException));
        return ResponseVO.fail(businessException.getErrorCode(),businessException.getErrorMessage());
    }

}
